package ciyuan.Item.SkillBook;

import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.SkillBookType;
import ciyuan.Player.Skill.SkillUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ArmorStandEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.server.ServerWorld;

import java.util.Vector;

/**
 * @author 150149
 */
public class WindSpell extends SkillBookType {

    public WindSpell() {
        super(new Properties().tab(ItemGroups.SkillBook));
    }

    @Override
    public void keyDown(PlayerEntity playerEntity) {
        SkillUtils.setSkillColdInterval(playerEntity, this, 25 * 1000);
        if (playerEntity.level.isClientSide) {
            return;
        }
        Vector<LivingEntity> hurts = new Vector<>();
        BlockPos blockPos = SkillUtils.getPlayerSee(playerEntity.level, playerEntity, RayTraceContext.FluidMode.SOURCE_ONLY, 10).getBlockPos();
        if (playerEntity.level instanceof ServerWorld) {
            for (double i=blockPos.getX()-3;i<blockPos.getX()+3;i=i+0.1) {
                for (double j=blockPos.getZ()-3;j<blockPos.getZ()+3;j=j+0.1) {
                    if (blockPos.distSqr(i,blockPos.getY()+1,j,false)>8 && blockPos.distSqr(i,blockPos.getY()+1,j,false)<9 ) {
                        ((ServerWorld)playerEntity.level).sendParticles(ParticleTypes.CLOUD, i , blockPos.getY()+1, j, 0, 0, 0.0D, 0, 0.0D);
                    }
                }
            }
        }

        for(LivingEntity livingentity : playerEntity.level.getEntitiesOfClass(LivingEntity.class, new AxisAlignedBB(
                blockPos.getX()+4,blockPos.getY()+3,blockPos.getZ()+4,blockPos.getX()-4,blockPos.getY()-3,blockPos.getZ()-4
        ))) {
            if (livingentity != playerEntity && !playerEntity.isAlliedTo(livingentity) && (!(livingentity instanceof ArmorStandEntity) || !((ArmorStandEntity)livingentity).isMarker()) && blockPos.distSqr(livingentity.getX(),livingentity.getY(),livingentity.getZ(),false) < 18.0D) {
                if (playerEntity.level instanceof ServerWorld) {
                    livingentity.addEffect(new EffectInstance(Effects.SLOW_FALLING,100,10));
                    livingentity.moveTo(livingentity.getX(),livingentity.getY()+2,livingentity.getZ());
                    hurts.add(livingentity);
                }
            }
        }
        SkillUtils.THREAD_POOL_EXECUTOR.execute(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<5;i++) {
                    for (LivingEntity livingEntity:hurts) {
                        if (livingEntity.isAlive()) {
                            livingEntity.hurt(DamageSource.playerAttack(playerEntity), 10);
                        }
                    }
                    SkillUtils.sleep(1000);
                }
            }
        });

        SkillUtils.saveSkillCastTime(playerEntity,this);
    }

    @Override
    public String getSkillCastName() {
        return "WindSpell";
    }

    @Override
    public ITextComponent getSkillDescription() {
        return new TranslationTextComponent("ciyuan.skill.desc.32");
    }

    @Override
    public ITextComponent getSkillName() {
        return new TranslationTextComponent("ciyuan.skill.name.32");
    }
}
